const express = require('express')
const addDataModel = require('../model/addDataModel')
const userDataModel = require('../model/userDataModel')
const vipUserModel = require('../model/vipUserModel')
const FBmodel = require('../model/FBmodel')
const orderModel = require('../model/orderModel')
const annModel = require('../model/annModel')
const ifToken = require('../utils/token')
const router = express.Router()

//获取所有帖子数据
router.post('/getUserFB', async (req, res) => {
    const data = await addDataModel.find()
    res.json({ code: '200', data })
})

//删除指定帖子数据
router.post('/deteleUserFB', async (req, res) => {
    const { _id } = req.body
    await addDataModel.deleteOne({ _id })
    await orderModel.deleteOne({ orderId: _id })
    const data = await addDataModel.find()
    res.json({ code: '200', data })
})

//搜索帖子数据
router.post('/SUserFB', async (req, res) => {
    const { data } = req.body
    if (data === '') {
        const newdata = await addDataModel.find()
        res.json({ code: '200', data: newdata })
        return
    }
    // 使用 $regex 进行模糊查询
    const regex = new RegExp(data);
    const newdata = await addDataModel.find({ title: regex })
    res.json({ code: '200', data: newdata })
})

//获取所有用户接单数据
router.post('/getUserJQ', async (req, res) => {
    const arr = []
    const data = await orderModel.find()
    for (const item of data) {
        const user = await userDataModel.findOne({ _id: item.mailId });
        const title = await addDataModel.findOne({ _id: item.orderId });
        arr.push({ _id: item._id, orderId: title.title, mailId: user.mail, name: user.name });
    }
    res.json({ code: '200', data: arr })
})

//获取所有用户账号数据
router.post('/getUser', async (req, res) => {
    const data = await userDataModel.find()
    res.json({ code: '200', data })
})

//获取所有用户会员
router.post('/getUserVip', async (req, res) => {
    const data = await vipUserModel.find()
    res.json({ code: '200', data })
})

//添加FB账号
router.post('/addFB', async (req, res) => {
    const { data, money } = req.body
    await FBmodel.create({ FB: data, money })
    const FBData = await FBmodel.find()
    res.json({ code: '200', data: FBData })
})

//获取FB账号
router.post('/getFB', async (req, res) => {
    const FBData = await FBmodel.find()
    res.json({ code: '200', data: FBData })
})

//删除FB账号
router.post('/deleteFB', async (req, res) => {
    const { _id } = req.body
    await FBmodel.deleteOne({ _id })
    const FBData = await FBmodel.find()
    res.json({ code: '200', data: FBData })
})

//添加公告
router.post('/addAnn', async (req, res) => {
    const { data, title } = req.body
    const annnum = await annModel.find()
    if (annnum.length >= 6) return res.json({ code: '300', msg: '最多可以添加6条数据' })
    await annModel.create({ ann: data, title })
    const ann = await annModel.find()
    res.json({ code: '200', data: ann })
})

//获取所有公告
router.post('/getAnn', async (req, res) => {
    const ann = await annModel.find()
    res.json({ code: '200', data: ann })
})

//删除公告
router.post('/deleteAnn', async (req, res) => {
    const { _id } = req.body
    await annModel.deleteOne({ _id })
    const FBData = await annModel.find()
    res.json({ code: '200', data: FBData })
})

module.exports = router